/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package screen;

import javax.microedition.lcdui.Graphics;

import mapDrawer.MapDrawer;
import geometry.MovingTransform;
import geometry.Rectangle;

public class MapScreenBuffer extends DrawingScreenBuffer{
	private static MovingTransform t = null;
	private static MapDrawer mapd = null;
	private static Rectangle secondPassArea = null;
	private static final int d = 10;
	private static int frame = 0;
	private static Rectangle all;
	private static Rectangle left;
	private static Rectangle right;
	private static Rectangle up;
	private static Rectangle down;
	ScreenGraphics gg = new ScreenGraphics();
	
	public MapScreenBuffer(int width,int height, MovingTransform transform, MapDrawer m){
		super(width, height);
		t = transform;
		mapd = m;
		all = new Rectangle(0,0,w, h);
		left = new Rectangle(0,0,d,h);
		right = new Rectangle(w-d,0,w,h);
		up = new Rectangle(0,0,w,d);
		down = new Rectangle(0,h-d,w,h);
	}

	
	public void shiftLeft(){
		t.translate(d, 0);
		for(int y=0;y<h;y++)
			System.arraycopy(buff, y*w, buff, y*w+d,w-d);
		drawFirstPass(left);
		if(secondPassArea==null)
			secondPassArea = new Rectangle(left);
		else{
			secondPassArea.bbxf+=d;
			secondPassArea.unite(left);
			secondPassArea.intersect(all);
		}
	}
	public void shiftRight(){
		t.translate(-d, 0);
		for(int y=0;y<h;y++)
			System.arraycopy(buff, d+y*w, buff, y*w, w-d);
		drawFirstPass(right);
		if(secondPassArea==null)
			secondPassArea = new Rectangle(right);
		else{
			secondPassArea.bbxi-=d;
			secondPassArea.unite(right);
			secondPassArea.intersect(all);
		}
	}
	public void shiftUp(){
		t.translate(0, d);
		for(int y=h;y>d;y--)
			System.arraycopy(buff, (y-d-1)*w, buff, (y-1)*w, w);
		drawFirstPass(up);
		if(secondPassArea==null)
			secondPassArea = new Rectangle(up);
		else{
			secondPassArea.bbyf+=d;
			secondPassArea.unite(up);
			secondPassArea.intersect(all);
		}
	}

	public void shiftDown(){
		t.translate(0, -d);
		for(int y=0;y<(h-d);y++)
			System.arraycopy(buff, (y+d)*w, buff, y*w, w);
		drawFirstPass(down);
		if(secondPassArea==null)
			secondPassArea = new Rectangle(down);
		else{
			secondPassArea.bbyi-=d;
			secondPassArea.unite(down);
			secondPassArea.intersect(all);
		}
	}
	
	public void zoom(long z){
		t.zoom(z);
		drawFirstPass(all);
		if(secondPassArea==null)
			secondPassArea = new Rectangle(all);
	}
	
	public void drawFirstPass(Rectangle area){
		setClip(area);
		clear(area);
		mapd.drawFirstPass(this, area, t);
	}
	
	public void drawSecondPass(){
		if(secondPassArea==null)
			secondPassArea = all;
		setClip(secondPassArea);
		mapd.drawSecondPass(this, secondPassArea, t);
		secondPassArea = null;
		frame = 0;
	}

	public void drawVolatilePass(Graphics g, Rectangle allArea) {
		gg.setGraphics(g);
		mapd.drawVolatilePass(gg, allArea, t, frame++);
	}
	
}
